#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char **argv)
{
	unsigned char buff[32]={0};
	int ret = 0,i;
	char *ip = "192.168.0.178";

	printf("buff addr[0-31] = %p,%p...%p(30),%p(31)\n",buff,buff+1,buff+30,buff+31);

	/**< 4,12,20,28 */
	sscanf(ip,"%d.%d.%d.%d",buff+4,buff+12,buff+20,buff+28);
	printf("buff value[4,12,20,28]:0x%0x,0x%0x,0x%0x,0x%0x\n",buff[4],buff[12],buff[20],buff[28]);
	printf("buff value[0-32]:\n");
	for(i=0;i<32;i++)
	{
		printf("0x%0x ",buff[i]);
	}
	printf("\n\n");

	/**< 4,5,6,7*/
	memset(buff,0,sizeof(buff));
	sscanf(ip,"%hhu.%hhu.%hhu.%hhu",buff+4,buff+5,buff+6,buff+7);
	printf("buff value[4,5,6,7]:0x%0x,0x%0x,0x%0x,0x%0x\n",buff[4],buff[5],buff[6],buff[7]);
	printf("buff value[0-32]:\n");
	for(i=0;i<32;i++)
	{
		printf("0x%0x ",buff[i]);
	}
	printf("\n\n");

	/**< 192.168*/
	memset(buff,0,sizeof(buff));
	sscanf("192.","%d.",buff+4);
	printf("buff value[4(192),0-32]:\n");
	for(i=0;i<32;i++)
	{
		printf("0x%0x ",buff[i]);
	}
	printf("\n");
	sscanf("168.","%d.",buff+5);
	printf("buff value[5(168),0-32]:\n");
	for(i=0;i<32;i++)
	{
		printf("0x%0x ",buff[i]);
	}
	printf("\n");
	sscanf("1.","%d.",buff+6);
	printf("buff value[6(1),0-32]:\n");
	for(i=0;i<32;i++)
	{
		printf("0x%0x ",buff[i]);
	}
	printf("\n");
	sscanf("178.","%d.",buff+7);
	printf("buff value[7(178),0-32]:\n");
	for(i=0;i<32;i++)
	{
		printf("0x%0x ",buff[i]);
	}
	printf("\n");
	sscanf("255.","%d.",buff+8);
	printf("buff value[8(255),0-32]:\n");
	for(i=0;i<32;i++)
	{
		printf("0x%0x ",buff[i]);
	}
	printf("\n\n");

	/**< use inet_addr/inet_network interface convert */
	in_addr_t addr = inet_addr(ip);
	printf("addr by inet_addr = [addr]%p,[value]0x%0x, %u\n",&addr, addr, addr);
	// cast convert address type
	unsigned char *paddr = (unsigned char *)&addr;
	printf("addr by char address: %p,%p,%p,%p\n",paddr,paddr+1,paddr+2,paddr+3);
	printf("addr by char value: 0x%0x,0x%0x,0x%0x,0x%0x\n",paddr[0],paddr[1],paddr[2],paddr[3]);
	printf("\n");

	addr = inet_network(ip);
	printf("addr by inet_network = [addr]%p,[value]0x%0x, %u\n",&addr, addr, addr);
	// cast convert address type
	paddr = (unsigned char *)&addr;
	printf("addr by char address: %p,%p,%p,%p\n",paddr,paddr+1,paddr+2,paddr+3);
	printf("addr by char value: 0x%0x,0x%0x,0x%0x,0x%0x\n",paddr[0],paddr[1],paddr[2],paddr[3]);

	return 0;
}



